import { DataTypes, Model } from 'sequelize'
import { sequelize } from '../../connect'
import { findAllValues, findOneRandomValue, findOneValue } from '../../public'

type ModelProps = {
  id: number
  qid: string /* 识别码 */
  name: string /* 名字 */
  uid: number /* 用户uid */
  status: number /* 状态 */
  register_time: Date /* 注册时间 */
  schedule: string /* 进度 */
  map?: string /* 位置 */
  coordinate_x?: string /* 坐标x */
  coordinate_y?: string /* 坐标y */
}

class user extends Model<ModelProps> {
  public static findAllValues = findAllValues
  public static findOneValue = findOneValue
  public static findOneRandomValue = findOneRandomValue
}

user.init(
  {
    id: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      allowNull: false,
      autoIncrement: true, // 自增
      comment: '用户id'
    },
    qid: {
      type: DataTypes.STRING(100),
      primaryKey: true,
      allowNull: false,
      comment: '用户识别码'
    },
    name: {
      type: DataTypes.STRING(20),
      primaryKey: true,
      allowNull: false,
      comment: '名字'
    },
    uid: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      allowNull: false,
      comment: '用户编号'
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '用户状态',
      defaultValue: 0 /* 0: 自由, 6: 战斗, 2: 删除 */
    },
    register_time: {
      type: DataTypes.DATE,
      allowNull: false,
      comment: '注册时间'
    },
    schedule: {
      type: DataTypes.STRING(255),
      allowNull: true,
      comment: '用户进度'
    },
    map: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '用户地图'
    },
    coordinate_x: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '用户坐标x'
    },
    coordinate_y: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '用户坐标y'
    }
  },
  {
    sequelize,
    tableName: 'user',
    freezeTableName: true,
    createdAt: false,
    updatedAt: false,
    indexes: [
      {
        unique: true,
        fields: ['id']
      },
      {
        unique: true,
        fields: ['name']
      },
      {
        unique: true,
        fields: ['qid']
      },
      {
        unique: true,
        fields: ['uid']
      }
    ]
  }
)

export { user }
